package com.yamanyar.weebill.view.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.yamanyar.weebill.domain.Project;
import com.yamanyar.weebill.service.ProjectService;

/**
 * This converter is spring managed bean due to need of loading the entity with
 * the given id.
 * 
 * Created in Jul 5, 2008.
 * 
 * @author Kaan Yamanyar
 */
@Component("projectConverter")
@Scope("request")
public class ProjectConverter implements Converter {
	private ProjectService projectService;

	@Autowired
	public ProjectConverter(ProjectService projectService) {
		this.projectService = projectService;
	}

	public Object getAsObject(FacesContext context, UIComponent component,
			String value) throws ConverterException {
		Project project = projectService.getProjectWithID(new Long(value));
		return project;
	}

	public String getAsString(FacesContext context, UIComponent component,
			Object value) throws ConverterException {
		if (value == null)
			return null;
		Project project = (Project) value;
		if (project.getId() == null)
			return null;
		return project.getId().toString();
	}

	public ProjectService getProjectService() {
		return projectService;
	}

	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}

}
